<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends GloableController {
	public function index(){
if(IS_POST){
            $data=I('post.');
            $info=D('admin')->login($data['name'],$data['password']);
            if($info==1){
                $this->success('登陆成功',U('Admin/index/list1'));
            }else{
                $this->error('登陆失败请重试');
            }
        }else{
            $this->display();
        }
    }
    public function list1(){
        $info=D('pay')->getlist();
        $this->assign("info1",$info);
        $this->display();
    }

    public function login(){
        if(IS_POST){
            $data=I('post.');
            $info=D('admin')->login($data['name'],$data['password']);
            if($info==1){
                $this->success('登陆成功',U('Admin/index/list1'));
            }else{
                $this->error('登陆失败请重试');
            }
        }else{
            $this->display();
        }
    }

    public function official(){
      $info=D('user')->add_user();
      $this->show($info);
  }

  public function zoon(){
      for($i=65;$i<91;$i++){
       $data[][]=chr($i);
   }
   $this->assign("data",$data);
   $this->display();
}

public function zoon1(){
  $info=D('user')->add_user1();
		//$this->show($info);
}

public function exportExcel($expTitle,$expCellName,$expTableData){
        $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
        $fileName = $_SESSION['account'].date('Y-m-d');//or $xlsTitle 文件名称可根据自己情况设定
        $cellNum = count($expCellName);
        $dataNum = count($expTableData);
        vendor("Classes.PHPExcel");

        $objPHPExcel = new \PHPExcel();
        $cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');
        
        /* $objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格*/
        for($i=0;$i<$cellNum;$i++){
        	$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'1', $expCellName[$i][1]); 
        	$objPHPExcel->getActiveSheet()->getColumnDimension($cellName[$i])->setWidth(20);
        } 
          // Miscellaneous glyphs, UTF-8   
        for($i=0;$i<$dataNum;$i++){
        	for($j=0;$j<$cellNum;$j++){
        		$objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+2), $expTableData[$i][$expCellName[$j][0]]);
        	}             
        }  
        
        header('pragma:public');
        header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
        header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  
        $objWriter->save('php://output'); 
        exit;   
    }
/**
     *
     * 导出Excel
     */
    public function export(){//导出Excel
    	$xlsName  = "goods";
    	$xlsCell  = array(
    		array('id','序号'),
    		array('phone','电话'), 
    		array('name','姓名'),
    		array('address','地址'),
    		array('number','数量'),
    		array('express','快递'),
    		array('expressid','单号'),
    		);
        $data['time']=date("Y-m-d",time());
        $data['state']=1;
        $xlsData= M('goods')->where($data)->select();
        $this->exportExcel($xlsName,$xlsCell,$xlsData);

    }

    public function impUser(){
    	if (!empty($_FILES)) {

    		$config=array(
    			'maxSize'    =>    3145728,
    			'rootPath'   =>    './Uploads/',
    			'savePath'   =>    '',
    			'saveName'   =>    array('uniqid',''),
    			'exts'       =>    array('xlsx','xls'),
    			'autoSub'    =>    true,
    			'subName'    =>    array('date','Ymd'),
    			);
          $upload = new \Think\Upload($config);// 实例化上传类
          $info   =   $upload->upload();
    if(!$info) {// 上传错误提示错误信息
    	$this->error($upload->getError());
    }else{// 上传成功
    	$this->success('上传成功！');
    }

    vendor("Classes.PHPExcel");
    $file_name='./Uploads/'.$info['import']['savepath'].$info['import']['savename'];

    $objReader = \PHPExcel_IOFactory::createReader('Excel5');
    $objPHPExcel = $objReader->load($file_name,$encode='utf-8');
    $sheet = $objPHPExcel->getSheet(0);
                $highestRow = $sheet->getHighestRow(); // 取得总行数
                $highestColumn = $sheet->getHighestColumn(); // 取得总列数
                for($i=2;$i<=$highestRow;$i++)
                {   
                	$data1['id']=  $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();  
                	/*$data['phone']=  $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue(); 
                	$data['name']=  $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue(); 
                	$data['address']=  $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
                	$data['number']=  $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();  */
                	$data['express']=  $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
                	$data['expressid']=  $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue(); 
                	$info=M('goods')->where($data1)->save($data);
                	
                } 
                if($info){
                	$this->success('导入成功！');
                }else{

                }
            }else
            {
            	$this->error("请选择上传的文件");
            }    

        }


        public function payment(){
            $data['id']=I('pk');
            $data2['user']=M('pay')->where($data)->getField('user');
            $data2['state']=1;
            $data1['payment']=1;
            $data1['time']=date("Y-m-d",time());
            $info=M('goods')->where($data2)->save($data1);
            $data3['payment']=1;
            $info=M('pay')->where($data)->save($data3);
            $this->redirect('Index/index');
        }

        public function activity(){
            if(IS_POST){
                $data=I('');
                $info=M('activity')->add($data);
            }else{
                $this->display();
            }
        }

        public function export1(){
            $this->display();
        }
    }
